package com.qczl.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qczl.common.core.utils.DateUtils;
import com.qczl.common.security.utils.SecurityUtils;
import com.qczl.vehicle.domain.Brand;
import com.qczl.vehicle.domain.Vehicles;
import com.qczl.vehicle.domain.vo.VehiclesVo;
import com.qczl.vehicle.mapper.BrandMapper;
import com.qczl.vehicle.mapper.VehiclesMapper;
import com.qczl.vehicle.service.IVehiclesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-09-20
 * @Description:
 * @Version: 1.0
 */
@Service
public class VehiclesServiceImpl extends ServiceImpl<VehiclesMapper, Vehicles> implements IVehiclesService {
    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 查询车辆列表
     *
     * @param vehiclesVo 查询条件
     * @return 车辆列表
     */
    @Override
    public List<VehiclesVo> selectVehicleList(VehiclesVo vehiclesVo) {
        return vehiclesMapper.selectVehicleList(vehiclesVo);
    }

    /**
     * 保存车辆信息，并关联品牌
     *
     * @param vehicles  车辆信息
     * @param brandName 品牌名称
     * @return 是否保存成功
     */
    @Override
    public boolean saveVehicleWithBrandName(Vehicles vehicles, String brandName) {
        // 创建查询条件包装器，用于根据品牌名称查询品牌信息
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        // 设置查询条件：品牌名称等于传入的brandName
        queryWrapper.eq(Brand::getName, brandName);
        // 根据品牌名称查询品牌信息
        Brand brand = brandMapper.selectOne(queryWrapper);
        // 如果未找到对应的品牌信息，则抛出异常
        if (brand == null) {
            throw new IllegalArgumentException("品牌不存在");
        }
        // 将查询到的品牌ID设置到车辆对象中
        vehicles.setBrandId(brand.getId());
        // 设置创建时间
        vehicles.setCreateTime(DateUtils.getNowDate());
        // 设置创建人
        vehicles.setCreateBy(SecurityUtils.getUsername());
        return this.save(vehicles);
    }

    /**
     * 更新车辆信息，并关联品牌
     *
     * @param vehicleId  车辆ID
     * @param vehiclesVo 车辆信息
     * @return 是否更新成功
     */
    @Override
    @Transactional
    public boolean updateVehicleWithBrandName(Long vehicleId, VehiclesVo vehiclesVo) {
        // 获取品牌名称
        String brandName = vehiclesVo.getBrandName();
        // 初始化品牌ID为null
        Long brandId = null;
        // 如果品牌名称不为空且不为空字符串
        if (brandName != null && !brandName.isEmpty()) {
            // 构造查询条件，根据品牌名称查询品牌信息
            LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Brand::getName, brandName);
            // 查询品牌信息
            Brand brand = brandMapper.selectOne(wrapper);
            // 如果品牌不存在
            if (brand == null) {
                throw new IllegalArgumentException("指定的品牌名称 '" + brandName + "' 不存在");
            }
            // 获取品牌ID
            brandId = brand.getId();
        }
        // 如果品牌名称为null
        else if (brandName == null) {
            throw new IllegalArgumentException("请指定品牌名称");
        }
        // 根据车辆ID获取车辆信息
        Vehicles vehiclesUpdate = this.getById(vehicleId);
        // 如果车辆不存在
        if (vehiclesUpdate == null) {
            throw new IllegalArgumentException("车辆不存在");
        }
        // 更新车辆的各项属性
        vehiclesUpdate.setModel(vehiclesVo.getModel());
        vehiclesUpdate.setPlateNumber(vehiclesVo.getPlateNumber());
        vehiclesUpdate.setYear(vehiclesVo.getYear());
        vehiclesUpdate.setColor(vehiclesVo.getColor());
        vehiclesUpdate.setDailyRate(vehiclesVo.getDailyRate());
        vehiclesUpdate.setStatus(vehiclesVo.getStatus());
        vehiclesUpdate.setImageUrl(vehiclesVo.getImageUrl());
        vehiclesUpdate.setUpdateTime(DateUtils.getNowDate());
        vehiclesUpdate.setUpdateBy(SecurityUtils.getUsername());
        // 如果品牌ID不为空，则更新品牌ID
        if (brandId != null) {
            vehiclesUpdate.setBrandId(brandId);
        }
        // 执行更新操作并返回结果
        return this.updateById(vehiclesUpdate);
    }
}
